<%@ page import="com.google.appengine.api.blobstore.BlobstoreServiceFactory" %>
<%@ page import="com.google.appengine.api.blobstore.BlobstoreService" %>
<%@ page import="com.google.appengine.api.users.User" %>
<%@ page import="com.google.appengine.api.users.UserService" %>
<%@ page import="com.google.appengine.api.users.UserServiceFactory" %>
<%@ page import="com.google.appengine.api.datastore.DatastoreService" %>
<%@ page import="com.google.appengine.api.datastore.DatastoreServiceFactory" %>
<%@ page import="com.google.appengine.api.datastore.Entity" %>
<%@ page import="com.MediaVaultTest.UserStore" %>
<%@ page import="com.MediaVaultTest.Media" %>
<%@ page import="com.google.appengine.api.datastore.Key" %>
<%@ page import="com.google.appengine.api.datastore.KeyFactory" %>
<%@ page import="com.google.appengine.api.datastore.PreparedQuery" %>
<%@ page import="com.google.appengine.api.datastore.Query" %>
<%@ page import="javax.jdo.JDOHelper" %>
<%@ page import="javax.jdo.PersistenceManager" %>
<%@ page import="javax.jdo.PersistenceManagerFactory" %>
<%@ page import="com.MediaVaultTest.PMF" %>
<%@ page import="javax.jdo.JDOObjectNotFoundException" %>
<%@ page import="java.util.LinkedList" %>
<%@ page import="com.MediaVaultTest.Format" %>
<%@ page import="com.MediaVaultTest.QuotaLogic" %>
<%@ page import="com.MediaVaultTest.GetAdds" %>

<%
    BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();
	GetAdds getAdds = new GetAdds();

%>

<!DOCTYPE HTML>
<html>

<head>
  <title>QUT Media Vault</title>
  <meta name="description" content="website description" />
  <meta name="keywords" content="website keywords, website keywords" />
  <meta http-equiv="content-type" content="text/html; charset=windows-1252" />
  <link rel="stylesheet" type="text/css" href="style/style.css" />
</head>

<body>
  <%
  	//Setup user service and data store
	 UserService userService = UserServiceFactory.getUserService();
	 User user = userService.getCurrentUser();
	 
	 //If user logged in
	 if (user != null) {
		 DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
		 PersistenceManager pm = PMF.get().getPersistenceManager();
		 
		 //Check if user system
		 String email =  user.getEmail();
		 
		 UserStore userStore = null;
		 Key key = KeyFactory.createKey(UserStore.class.getSimpleName(), email);
		 try { 
			 userStore = pm.getObjectById(UserStore.class, key);
			 //See if anything is returned\
		 } catch (JDOObjectNotFoundException e) {
			 userStore = null;
		 }
		 
		
		 //if not create them
		 if(userStore==null){
			 int up = 0;
			 int down = 0;
			 int store = 0;
			 userStore = new UserStore(email, up, down, store);
			 //userStore = new UserStore();
			 userStore.setKey(key);
			 try {
				 pm.makePersistent(userStore);
			 } finally {
				 
			 }
		} else {
			
		}
	
  %>
  <div id="main">
    <div id="header">
      <div id="logo">
        <div id="logo_text">
          <!-- class="logo_colour", allows you to change the colour of the text -->
          <h1><a href="index.jsp">Media Vault<span class="logo_colour">QUT</span></a></h1>
          <h2>Media Centre. Currently in Alpha</h2>
        </div>
        <div id="ads_top">
			<%=
	        	getAdds.getTopBanner()
	        %>
        </div>
      </div>
      <div id="menubar">
        <ul id="menu">
          <!-- put class="selected" in the li tag for the selected page - to highlight which page you're on -->
          <li><a href="/">General</a></li>
		  <li><a href="photos.jsp">Photos</a></li>
          <li><a href="music.jsp">Music</a></li>
          <li><a href="movies.jsp">Movies</a></li>
          <li><a href="tv_shows.jsp">TV Shows</a></li>
		  <li class="selected"><a href="adminPanel.jsp">Admin Panel</a></li>
		  <li><a href="userPanel.jsp">User Panel</a></li>
		  <li><a href="<%= userService.createLogoutURL(request.getRequestURI()) %>">Sign Out</a></li>
        </ul>
      </div>
    </div>
    <div id="site_content">
    <div class="left_sidebar">
    <h1>Quota Remaining</h1>
    <table>
    <% QuotaLogic quotaLogic = new QuotaLogic(); %>
    <tr><td>Upload</td><td><%= quotaLogic.getMBUploadRemaining(userStore) %>MB</td></tr>
    <tr><td>Download</td><td><%= quotaLogic.getMBDownloadRemaining(userStore) %>MB</td></tr>
    <tr><td>Storage</td><td><%= quotaLogic.getMBStoreRemaining(userStore) %>MB</td></tr>
    </table>
	    </div>
      <div class="sidebar">
      <h1>Search</h1>
      <form method="post" action="#" id="search_form">
        <p>
          <input class="search" type="text" name="search_field" value="Enter keywords....." />
          <input name="search" type="image" style="border: 0; margin: 0 0 -5px 0;" src="style/search.png" alt="Search" title="Search" />
        </p>
      </form>
        <div id="ads_left">
	        <%=
	        	getAdds.getRightBanner()
	        %>
        </div>
        
      </div>
      <div id="content">
        <h1>Admin Panel</h1>
      </div>
    </div>
    <div id="footer">
      <p><a href="/">Home</a> | <a href="/">LOL Cats</a> | <a href="/">Contact Us</a></p>
      <p>Copyright &copy; IT CROWD | <a href="http://validator.w3.org/check?uri=referer">HTML5</a> | <a href="http://jigsaw.w3.org/css-validator/check/referer">CSS</a> | <a href="http://www.html5webtemplates.co.uk">design dervived from HTML5webtemplates.co.uk</a></p>
    </div>
  </div>
  <%
  pm.close();
    } else { 
  %>
        <p>Hello!
		<a href="<%= userService.createLoginURL(request.getRequestURI()) %>">Sign in</a>
		to access media vault.</p>
  <% } %>
</body>
</html>
